package com.carlinfo.hospital.dao.impl;

import com.carlinfo.hospital.dao.MedicineDao;
import com.carlinfo.hospital.pojo.Medicine;
import com.carlinfo.hospital.pojo.Person;
import com.carlinfo.hospital.util.DataSourceUtil;
import com.carlinfo.hospital.util.PageInfoUtil;
import com.carlinfo.hospital.util.ReflectUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class MedicineDaoImpl implements MedicineDao
{
    //id name  price count typeId status  createTime updateTime publicTime
    @Autowired
    private DataSourceUtil dataSourceUtil;
    DataSource dataSource = dataSourceUtil.getInstance();
    QueryRunner queryRunner = new QueryRunner(dataSource);
    String sql = "";

    @Override
    public int saveOneDao(Medicine medicine)
    {
        //id name  price count typeId status  createTime updateTime publicTime
        sql = "insert into medicine values(null,?,?,?,?,?,?,?,?)";
        DataSource dataSource = dataSourceUtil.getInstance();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        try
        {
            Object[] zh = ReflectUtil.zh(medicine);
            int execute = queryRunner.execute(sql, zh);
            System.out.println(execute);
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Medicine> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
    {
        sql = "select * from medicine where 1=1 ";
        List<Object> list = new ArrayList<Object>();
        if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
        {
            sql += " and name like ? ";
            list.add("%" + condMap.get("keyword") + "%");
        }
        if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
        {
            sql += " and status like ? ";
            list.add("%" + condMap.get("status") + "%");
        }
        if (condMap.get("typeId") != null && !"".equalsIgnoreCase(condMap.get("typeId") + ""))
        {
            sql += " and typeId = ? ";
            list.add(condMap.get("typeId"));
        }
        if (condMap.get("stDate") != null && !"".equalsIgnoreCase(condMap.get("edDate") + ""))
        {
            sql += " and (publicTime>? and publicTime<?)";
            list.add(condMap.get("stDate"));
            list.add(condMap);
        }
        if (pageInfoUtil != null)
        {
            sql += " limit "+pageInfoUtil.getCurrentRecord() +","+pageInfoUtil.getPageSize();
        }
        System.out.println("sql:" + sql);
        String[] s = new String[list.size()];
        String[] strings = list.toArray(s);
        try
        {
            List<Medicine> res = queryRunner.query(sql, new BeanListHandler<Medicine>(Medicine.class), strings);
            return res;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Medicine selectOneDao(Map<String, Object> condMap)
    {
        List<Object> list = new ArrayList<Object>();
        if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null)
        {
            sql = "select * from medicine where name= ? ";
            list.add(condMap.get("name"));
        }
        if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null)
        {
            sql = "select * from medicine where id= ? ";
            list.add(condMap.get("id"));
        }
        try
        {
            Medicine res = queryRunner.query(sql, new BeanHandler<Medicine>(Medicine.class), list.get(0));
            return res;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int updateOneDao(Medicine medicine)
    {
        //id name  price count typeId status  createTime updateTime publicTime
        sql = "update medicine set name=?,price=?,count=?,typeId=?,status=?,createTime=? ,updateTime=?,publicTime=? where id =?";
        try
        {
            int execute = queryRunner.execute(sql, ReflectUtil.zh(medicine));
            return execute;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteOneDao(Map<String, Object> condMap)
    {
        List<Object> list = new ArrayList<Object>();
        if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null)
        {
            sql = "delete * from medicine where name= ? ";
            list.add(condMap.get("name"));
        }
        if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null)
        {
            sql = "delete * from medicine where id= ? ";
            list.add(condMap.get("id"));
        }
        String[] strings = new String[list.size()];
        try
        {
            int execute = queryRunner.execute(sql, list.toArray(strings));
            return execute;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;
    }
}
